package com.mdd.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.yulichang.query.MPJQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mdd.admin.validate.commons.PageValidate;
import com.mdd.admin.service.IEmailRuleConfigurationService;
import com.mdd.admin.validate.EmailRuleConfigurationCreateValidate;
import com.mdd.admin.validate.EmailRuleConfigurationUpdateValidate;
import com.mdd.admin.validate.EmailRuleConfigurationSearchValidate;
import com.mdd.admin.vo.EmailRuleConfigurationListedVo;
import com.mdd.admin.vo.EmailRuleConfigurationDetailVo;
import com.mdd.common.config.GlobalConfig;
import com.mdd.common.core.PageResult;
import com.mdd.common.entity.EmailRuleConfiguration;
import com.mdd.common.mapper.EmailRuleConfigurationMapper;
import com.mdd.common.util.ListUtils;
import com.mdd.common.util.TimeUtils;
import com.mdd.common.util.UrlUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.*;

/**
 * 规则邮箱配置实现类
 * @author LikeAdmin
 */
@Service
public class EmailRuleConfigurationServiceImpl implements IEmailRuleConfigurationService {
        
    @Resource
    EmailRuleConfigurationMapper emailRuleConfigurationMapper;

    /**
     * 规则邮箱配置列表
     *
     * @author LikeAdmin
     * @param pageValidate 分页参数
     * @param searchValidate 搜索参数
     * @return PageResult<EmailRuleConfigurationListedVo>
     */
    @Override
    public PageResult<EmailRuleConfigurationListedVo> list(PageValidate pageValidate, EmailRuleConfigurationSearchValidate searchValidate) {
        Integer page  = pageValidate.getPageNo();
        Integer limit = pageValidate.getPageSize();

        QueryWrapper<EmailRuleConfiguration> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete", 0);
        queryWrapper.orderByDesc("id");

        emailRuleConfigurationMapper.setSearch(queryWrapper, searchValidate, new String[]{
            "=:ruleTyp@rule_typ:str",
        });

        IPage<EmailRuleConfiguration> iPage = emailRuleConfigurationMapper.selectPage(new Page<>(page, limit), queryWrapper);

        List<EmailRuleConfigurationListedVo> list = new LinkedList<>();
        for(EmailRuleConfiguration item : iPage.getRecords()) {
            EmailRuleConfigurationListedVo vo = new EmailRuleConfigurationListedVo();
            BeanUtils.copyProperties(item, vo);
            list.add(vo);
        }

        return PageResult.iPageHandle(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), list);
    }

    /**
     * 规则邮箱配置详情
     *
     * @author LikeAdmin
     * @param id 主键参数
     * @return EmailRuleConfiguration
     */
    @Override
    public EmailRuleConfigurationDetailVo detail(Integer id) {
        EmailRuleConfiguration model = emailRuleConfigurationMapper.selectOne(
                new QueryWrapper<EmailRuleConfiguration>()
                    .eq("id", id)
                    .eq("is_delete", 0)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在");

        EmailRuleConfigurationDetailVo vo = new EmailRuleConfigurationDetailVo();
        BeanUtils.copyProperties(model, vo);
        return vo;
    }

    /**
     * 规则邮箱配置新增
     *
     * @author LikeAdmin
     * @param createValidate 参数
     */
    @Override
    public void add(EmailRuleConfigurationCreateValidate createValidate) {
        EmailRuleConfiguration model = new EmailRuleConfiguration();
        model.setRuleTyp(createValidate.getRuleTyp());
        model.setTypeNotification(createValidate.getTypeNotification());
        model.setTransactionType(createValidate.getTransactionType());
        model.setTransactionStatus(createValidate.getTransactionStatus());
        model.setMailLabelType(createValidate.getMailLabelType());
        model.setApplicant(createValidate.getApplicant());
        model.setTheme(createValidate.getTheme());
        model.setRecipients(createValidate.getRecipients());
        model.setCopy(createValidate.getCopy());
        model.setBlindCarbonCopy(createValidate.getBlindCarbonCopy());
        emailRuleConfigurationMapper.insert(model);
    }

    /**
     * 规则邮箱配置编辑
     *
     * @author LikeAdmin
     * @param updateValidate 参数
     */
    @Override
    public void edit(EmailRuleConfigurationUpdateValidate updateValidate) {
        EmailRuleConfiguration model = emailRuleConfigurationMapper.selectOne(
                new QueryWrapper<EmailRuleConfiguration>()
                    .eq("id",  updateValidate.getId())
                    .eq("is_delete", 0)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        model.setId(updateValidate.getId());
        model.setRuleTyp(updateValidate.getRuleTyp());
        model.setTypeNotification(updateValidate.getTypeNotification());
        model.setTransactionType(updateValidate.getTransactionType());
        model.setTransactionStatus(updateValidate.getTransactionStatus());
        model.setMailLabelType(updateValidate.getMailLabelType());
        model.setApplicant(updateValidate.getApplicant());
        model.setTheme(updateValidate.getTheme());
        model.setRecipients(updateValidate.getRecipients());
        model.setCopy(updateValidate.getCopy());
        model.setBlindCarbonCopy(updateValidate.getBlindCarbonCopy());
        emailRuleConfigurationMapper.updateById(model);
    }

    /**
     * 规则邮箱配置删除
     *
     * @author LikeAdmin
     * @param id 主键ID
     */
    @Override
    public void del(Integer id) {
        EmailRuleConfiguration model = emailRuleConfigurationMapper.selectOne(
                new QueryWrapper<EmailRuleConfiguration>()
                    .eq("id", id)
                    .eq("is_delete", 0)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        model.setIsDelete(1);
        model.setDeleteTime(String.valueOf(System.currentTimeMillis() / 1000));
        emailRuleConfigurationMapper.updateById(model);
    }

}
